package com.gj.demojuc.chap07;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * 直接交付 不是一个真正的队列没有容量
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new SynchronousQueue<>();
//        queue.put("a");
//        System.out.println(queue.take());
        new Thread(()->{
            try {
                String data = UUID.randomUUID().toString().substring(0, 8);
                System.out.println("生产:" + data);
                queue.put(data);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"生产者").start();

        new Thread(()->{
            try {
                String result = queue.take();
                System.out.println("消费了:" + result);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"消费者").start();
    }
}
